package calvin.study.rabbitmq.advanced.return1;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ReturnListener;

import calvin.study.rabbitmq.base.RabbitMqBase;

public class Producer extends RabbitMqBase {

	private final static String EXCHANGE_TYPE = "topic";

	public static void main(String[] args) {

		// 1 创建ConnectionFactory
		ConnectionFactory connectionFactory = getConnectionFactory();

		
		
		try {
			// 2 获取C onnection
			Connection connection = connectionFactory.newConnection();

			// 3 通过Connection创建一个新的Channel
			Channel channel = connection.createChannel();
			
			String exchangeName = "return_topic_exchange";
			String routingKey = "return.#";
			String queueName = "return_queue";

			String msg = "Hello RabbitMQ Return Message";

			channel.addReturnListener(new ReturnListener() {

				@Override
				public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
						AMQP.BasicProperties properties, byte[] body) throws IOException {
					System.out.println("---------handle  return----------");
					//响应码 312
					System.out.println("replyCode: " + replyCode);
					//NO_ROUTE
					System.out.println("replyText: " + replyText);
					System.out.println("exchange: " + exchange);
					System.out.println("routingKey: " + routingKey);
					System.out.println("properties: " + properties);
					System.out.println("body: " + new String(body));

				}
			});
			
			channel.basicPublish(exchangeName, routingKey, true, null, msg.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
